package com.example.testtouch2;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import com.demo.androidgame.fx.Game;
import com.demo.androidgame.fx.Input.TouchEvent;
import com.demo.androidgame.fx.gl.Camera2D;
import com.demo.androidgame.fx.gl.SpriteBatcher;
import com.demo.androidgame.fx.impl.GLScreen;
import com.demo.androidgame.fx.math.OverlapTester;
import com.demo.androidgame.fx.math.Rectangle;
import com.demo.androidgame.fx.math.Vector2;

public class HighscoresScreen extends GLScreen {
    Camera2D guiCam;
    SpriteBatcher batcher;
    Rectangle backBounds;
    Vector2 touchPoint;
    String[] highScores;  
    String[] highPoints;
    float xOffset = 0;
    Rectangle goMenu;
	static boolean nfcCheck = false;

    public HighscoresScreen(Game game) {
        super(game);
        
        guiCam = new Camera2D(glGraphics, 320, 480);
//        backBounds = new Rectangle(0, 0, 64, 64);
        touchPoint = new Vector2();
        batcher = new SpriteBatcher(glGraphics, 100);
        goMenu = new Rectangle(160, 30, 160, 50);
        highScores = new String[5];        
        for(int i = 0; i < 5; i++) {
            highScores[i] = (i + 1) + ". " + Settings.highscores[i];
            xOffset = 30.0f + Math.max(highScores[i].length() * Assets.font3.glyphWidth, xOffset);
        }
        highPoints = new String[5];
        for(int i = 0; i < 5; i++){
        	highPoints[i] = (i+1) + "." + Settings.highpoints[i];
        	xOffset = 30.0f + Math.max(highPoints[i].length() * Assets.font.glyphWidth, xOffset);
        	
        }
        xOffset = 160 - xOffset / 2 + Assets.font3.glyphWidth / 2;
    }    

    @Override
    public void update(float deltaTime) {
        List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
        game.getInput().getKeyEvents();
        int len = touchEvents.size();
        for(int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);
            if(event.type == TouchEvent.TOUCH_UP) {
            touchPoint.set(event.x, event.y);
            guiCam.touchToWorld(touchPoint);
            
            if(OverlapTester.pointInRectangle(goMenu, touchPoint)) {
                
                game.setScreen(new menuScreen(game));
                return;
            }
            }
        }
    }

    @Override
    public void present(float deltaTime) {
        GL10 gl = glGraphics.getGL();        
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        guiCam.setViewportAndMatrices();
        
        gl.glEnable(GL10.GL_TEXTURE_2D);
        
        batcher.beginBatch(Assets.scoreback);
        batcher.drawSprite(160, 240, 320, 480, Assets.scorebackRegion);
        batcher.endBatch();
        
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
      
        if(nfcCheck == false){
        batcher.beginBatch(Assets.score320);
        float y = 140;
        for(int i = 4; i >= 0; i--) {
            Assets.font3.drawText(batcher, highScores[i], xOffset+60, y);
          y += Assets.font3.glyphHeight;    
            batcher.endBatch();}}
            if(nfcCheck == true){
            	batcher.beginBatch(Assets.score320);
            	 float y = 140;
                 for(int i = 4; i >= 0; i--) {
                     Assets.font3.drawText(batcher, highScores[i], xOffset+60, y);
            Assets.font3.drawText(batcher, highPoints[i], xOffset+220, y);
            y += Assets.font3.glyphHeight;
            batcher.endBatch();}}
        
            
            if(nfcCheck == true){
           	 batcher.beginBatch(Assets.atlas);
           	batcher.drawSprite(240, 240, 80, 30, Assets.highpoint);
           	 batcher.endBatch();  
           };
        batcher.beginBatch(Assets.atlas);
        batcher.drawSprite(100, 240, 80, 30, Assets.highscore);
        batcher.drawSprite(210, 30,  100, 30, Assets.goMenu);
        batcher.endBatch();  
        
        gl.glDisable(GL10.GL_BLEND);
    }
    
    @Override
    public void resume() {        
    }
    
    @Override
    public void pause() {        
    }

    @Override
    public void dispose() {
    }
}
